package com.ruoyi.shopping.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.shopping.domain.CommodityImages;
import com.ruoyi.shopping.service.ICommodityImagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品图片Controller
 * 
 * @author ysq
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/shopping/commodityImages")
public class CommodityImagesController extends BaseController
{
    @Autowired
    private ICommodityImagesService commodityImagesService;

    /**
     * 查询商品图片列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityImages:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommodityImages commodityImages)
    {
        startPage();
        List<CommodityImages> list = commodityImagesService.selectCommodityImagesList(commodityImages);
        return getDataTable(list);
    }

    /**
     * 导出商品图片列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityImages:export')")
    @Log(title = "商品图片", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityImages commodityImages)
    {
        List<CommodityImages> list = commodityImagesService.selectCommodityImagesList(commodityImages);
        ExcelUtil<CommodityImages> util = new ExcelUtil<CommodityImages>(CommodityImages.class);
        util.exportExcel(response, list, "商品图片数据");
    }

    /**
     * 获取商品图片详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityImages:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(commodityImagesService.selectCommodityImagesById(id));
    }
    @PreAuthorize("@ss.hasPermi('shopping:commodityImages:query')")
    @GetMapping(value = "/image/{id}")
    public AjaxResult getImages(@PathVariable("id") Long id)
    {
        return success(commodityImagesService.selectByCommodityId(id));
    }

    /**
     * 新增商品图片
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityImages:add')")
    @Log(title = "商品图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommodityImages commodityImages)
    {
        return toAjax(commodityImagesService.insertCommodityImages(commodityImages));
    }


    /**
     * 修改商品图片
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityImages:edit')")
    @Log(title = "商品图片", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommodityImages commodityImages)
    {
        return toAjax(commodityImagesService.updateCommodityImages(commodityImages));
    }

    /**
     * 删除商品图片
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityImages:remove')")
    @Log(title = "商品图片", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(commodityImagesService.deleteCommodityImagesByIds(ids));
    }
}
